package com.hermit.qmst.UI.activitys;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hermit.qmst.R;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.RequestHttp;
import com.hermit.qmst.tools.SelectPicUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.util.KeyValue;
import org.xutils.http.RequestParams;
import org.xutils.http.body.MultipartBody;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/1/5 0005.
 */
@ContentView(R.layout.new_activity_up_img)
public class UpdateUserPhotoActivity extends Activity {

    @ViewInject(R.id.iv_back)
    ImageView mIvBack;
    @ViewInject(R.id.tv_title)
    TextView mTvTitle;
    @ViewInject(R.id.iv_user_photo)
    ImageView mIvUserPhoto;
    @ViewInject(R.id.bt_save)
    Button mBtSave;
    private String uploadUrl = Common.mUrl + "szdh/userInfo.php?type=pic&uid=";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTvTitle.setText("头像设置");
        mIvUserPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectPicUtil.showPicDialog(UpdateUserPhotoActivity.this);
            }
        });

        mBtSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uploadImage();
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        Bitmap bitmap = SelectPicUtil.onActivityResult(UpdateUserPhotoActivity.this, requestCode, requestCode, data);
//        if (bitmap != null) {
//            mIvUserPhoto.setImageBitmap(bitmap);
//        }
//    }
        Bitmap bm = SelectPicUtil.onActivityResult(this, requestCode, resultCode, data, 0, 0, 0, 0);
        if (bm != null) {
            mIvUserPhoto.setImageBitmap(bm);
        }
    }

    private void uploadImage() {
        RequestParams params = new RequestParams(uploadUrl + Common.mAgentId);
        List<KeyValue> list = new ArrayList<>();
        params.setAsJsonContent(true);
        list.add(new KeyValue("fileName", new File(SelectPicUtil.img_url)));
        MultipartBody body = new MultipartBody(list, "UTF-8");
        params.setRequestBody(body);
        RequestHttp.uploadImage(UpdateUserPhotoActivity.this, params, new RequestHttp.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject) {
                try {
                    if (jsonObject.getBoolean("state")) {
                        Toast.makeText(UpdateUserPhotoActivity.this, "头像更新成功", Toast.LENGTH_SHORT).show();
                        UserInfoActivity.photeUrl = SelectPicUtil.img_url;
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
